﻿using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Examinee
{
    public partial class Examinee_ExameeSearchForm_List : BaseForm
    {
        BLL.ServiceExaminee serviceExam = new BLL.ServiceExaminee();
        public Examinee_ExameeSearchForm_List()
        {
            InitializeComponent();

            gridView1.TopRowChanged += gridView1_TopRowChanged;
        }

        int currentIndex = 1;
        int currentCount = -1;
        int totalCount = -1;
        int pageSize = 50;
        private void gridView1_TopRowChanged(object sender, EventArgs e)
        {
            if (gridView1.IsRowVisible(currentCount - 10) == DevExpress.XtraGrid.Views.Grid.RowVisibleState.Visible)
            {
                int MaxPageIndex = ((totalCount - 1) / pageSize) + 1;
                int pageIndex = ++currentIndex;
                if (pageIndex > MaxPageIndex)
                {
                    return;
                }
                BindInfo(pageIndex);
            }
        }

        private void benQuery_Click(object sender, EventArgs e)
        {
            dtDataSource = null;
            BindInfo(1);
        }

        #region 绑定gridControl1
        DataTable dtDataSource = null;
        private void BindInfo(int currentIndexData)
        {
            decimal feeMonty = 0;
            DataTable dt = serviceExam.GetExamineeOfAllInfo_ByBaseInfo(txtName.Text, txtNMT_SignUpID.Text, txtIdCard.Text,
                currentIndexData.ToSafeString(), pageSize.ToSafeString(), out totalCount, out feeMonty, Utility.Helper.SessionerHelper.SystemYear);

            if (dt != null)
            {
                if (dtDataSource == null)
                {
                    dtDataSource = dt.Clone();
                }
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["Sex"].ToString() == "1")
                    {
                        dr["Sex"] = "男";
                    }
                    else
                    {
                        dr["Sex"] = "女";
                    }
                    dtDataSource.ImportRow(dr);
                }
            }
            gridControl1.DataSource = dtDataSource;
            gridView1.TopRowChanged -= gridView1_TopRowChanged;
            gridControl1.RefreshDataSource();
            if (dt != null)
            {
                currentCount = dtDataSource.Rows.Count;
                if (dt.Rows.Count == pageSize)
                {
                    gridView1.TopRowChanged += gridView1_TopRowChanged;
                }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            gridView1.IndicatorWidth = 40;
            gridView1.PreviewIndent = 0;
            gridView1.CustomDrawRowIndicator += gridView1_CustomDrawRowIndicator;
            base.OnLoad(e);
        }

        void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = HorzAlignment.Far;
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        #endregion

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedValue() != null)
            {
                GridView rows = sender as GridView;
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string strNMT_SignUpID = rows.Rows[inteurm[0]].DataRow["NMT_SignUpID"].ToString();
                    Examinee_ExameeDetailForm obj = new Examinee_ExameeDetailForm();
                    obj.strNMT_SignUpID = strNMT_SignUpID;
                    obj.strType = "Siged";
                    obj.Run();
                }
            }
        }

        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                dtDataSource = null;
                BindInfo(1);
            }
        }
    }
}
